package com.market.controller.wx;

import com.market.bean.MarketComment;
import com.market.bean.MarketOrderGoods;
import com.market.bean.bo.wx.WxOrderCommentBo;
import com.market.bean.common.BaseRespVo;
import com.market.bean.common.CommonData;
import com.market.bean.vo.wx.WxOrderDetailVo;
import com.market.bean.vo.wx.WxOrderListVo;
import com.market.service.wx.WxOrderService;
import com.market.bean.bo.wx.WxOrderSubmitBo;
import com.market.bean.vo.wx.WxOrderSubmitVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author 2731235517@qq.com 谢畅
 * @since 2022/12/31 20:14
 */

@RestController
@RequestMapping("wx/order")
public class WxOrderController {

    @Autowired
    WxOrderService orderService;

    @RequestMapping("list")
    public BaseRespVo list(Short showType, Integer page, Integer limit) {
        CommonData<WxOrderListVo> data = orderService.list(showType, page, limit,null);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("detail")
    public BaseRespVo detail(Integer orderId) {
        WxOrderDetailVo data = orderService.detail(orderId);
        return BaseRespVo.ok(data);
    }

    @RequestMapping("submit")
    public BaseRespVo submit(@RequestBody WxOrderSubmitBo wxOrderSubmitBo) {
        WxOrderSubmitVo wxOrderSubmitVo = null;
        try {
            wxOrderSubmitVo = orderService.submit(wxOrderSubmitBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("操作失败");
        }
        return BaseRespVo.ok(wxOrderSubmitVo);
    }

    @RequestMapping("prepay")
    public BaseRespVo prepay(@RequestBody Map map) {
        int code = orderService.prepay((Integer) map.get("orderId"));
        if (code == 404) {
            return BaseRespVo.invalidParameter("支付失败");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("cancel")
    public BaseRespVo cancel(@RequestBody Map map) {
        try {
            orderService.cancel((Integer) map.get("orderId"));
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("操作失败");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("refund")
    public BaseRespVo refund(@RequestBody Map map) {
        /*String s = "{orderId: 20}";
        int i = s.indexOf(":");
        String orderId = s.substring(i + 2, i + 4);*/
        try {
            orderService.refund((Integer) map.get("orderId"));
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("操作失败");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("confirm")
    public BaseRespVo confirm(@RequestBody Map map) {
        try {
            orderService.confirm((Integer) map.get("orderId"));
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("操作失败");
        }
        return BaseRespVo.ok();
    }

    @RequestMapping("goods")
    public BaseRespVo goods(Integer orderId, Integer goodsId) {
        MarketOrderGoods data = orderService.goods(orderId, goodsId);
        return BaseRespVo.ok(data);
    }

    /*content: "very goods"
    hasPicture: false
    orderGoodsId: 512
    picUrls: []
    star: 5*/
    @RequestMapping("comment")
    public BaseRespVo comment(@RequestBody WxOrderCommentBo wxOrderCommentBo) {
        MarketComment marketComment = orderService.comment(wxOrderCommentBo);
        return BaseRespVo.ok(marketComment);
    }

    @RequestMapping("delete")
    public BaseRespVo delete(@RequestBody Map map) {
        orderService.delete((Integer) map.get("orderId"));

        return BaseRespVo.ok();
    }
}
